package com.study.samsung.Dao;

import com.study.samsung.Entity.User;
import com.study.samsung.Utils.RowMapper;
import com.study.samsung.Utils.SQLHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class UserDAO {
    private SQLHelper helper = new SQLHelper();

    public User selectByTel(String account){
        String sql = "select * from user where account=?";
        return helper.one(sql,new UserRowMapper(),account);
    }
    public Integer enroll( String nickname,
                           String uname,
                           String sex,
                           String uphone,
                           String birth,
                           String account,
                           String upwd){
        String sql = "insert into user values(?,?,?,?,?,?,?,?,?,?)";
        return helper.insert(sql,null,nickname,uname,sex,uphone,birth,"img/personal.png",account,upwd,1);
    }

    public Integer doUpdate(User u){
        String sql = "update user set upicture=?,nickname=?,sex=?,birth=? where uid=?";
//        报错为null
        return helper.update(sql,u.getUpicture(),u.getNickname(),u.getSex(),u.getBirth(),u.getUid());
    }
    public User dophone(String uphone){
        String sql = "select * from user where uphone=?";
        return helper.one(sql,new UserRowMapper(),uphone);
    }


    class UserRowMapper implements RowMapper<User>{

        @Override
        public User map(ResultSet rs) throws SQLException {
            return new User(rs.getInt("uid"),
                            rs.getString("nickname"),
                            rs.getString("uname"),
                            rs.getString("sex"),
                            rs.getString("uphone"),
                            rs.getDate("birth"),
                            rs.getString("upicture"),
                            rs.getString("account"),
                            rs.getString("upwd"),
                            rs.getBoolean("isdel"));
        }
    }

    //img/xxxxxx/202304151621386407.jpg
}
